<template>
	<view class="content_box">
		<view class="top_header" style="padding-top: 24rpx; margin-bottom: 24rpx">
			<view class="search_card">
				<view class="left_s">
					<uni-easyinput
						prefixIcon="search"
						v-model="keyword"
						@clear="searchValue"
						placeholderStyle="font-size:28rpx;"
						:inputBorder="true"
						placeholder="输入返厂单号或条码查询"
					></uni-easyinput>
					<view class="search_btn" @click="searchValue">
						<!-- <uni-icons type="search" size="20" color="#fff"></uni-icons> -->
						搜索
					</view>
				</view>
				<!-- <view class="phone_btn" @click="onPhone">
					查询
				</view> -->
			</view>

			<view class="segmented-control">
				<view
					class="segmented-control__text"
					:class="{'segmented-control__item--text': current == item.value}"
					v-for="(item, index) in items"
					:key="index"
					@click="onClickItem(item.value, index)"
				>
					<span class="text">{{ item.title }}</span>
					<span
						class="red_num"
						v-if="OrderStatusQty && setOrderNum(item.value) != 0"
					>
						{{ setOrderNum(item.value) }}
					</span>
				</view>
			</view>
		</view>
		<view class="main_bg">
			<scroll-view
				:refresher-triggered="triggered"
				refresher-background="transparent"
				@refresherpulling="refresherpulling"
				refresher-enabled="true"
				scroll-y="true"
				@scrolltolower="scrolltolower"
				class="scroll_y"
				@refresherrefresh="onRefresh"
			>
				<view class="content-list">
					<!-- phone2 -->
					<view
						v-if="PageTotalData[current2].dataList.length > 0"
						class="content-list_item"
					>
						<view
							class="c_item"
							v-for="(item, index) in PageTotalData[current2].dataList"
							:key="index"
						>
							<p @click="goPage(item.id)">
								<view class="sty_title">
									<view class="workNo">
										{{ item.repairNo }}
										<image
											class="btn_c"
											@click.stop="copy(item.repairNo)"
											src="/static/copy.svg"
											mode=""
										></image>
									</view>
									<span
										class="status"
										:class="
											item.status == 1 ||
											item.status == 2 ||
											item.status == 3 ||
											item.status == 6
												? 'status_123'
												: item.status == 4 || item.status == 5
												? 'status_4'
												: item.status == 8
												? 'status_6'
												: ''
										"
									>
										{{
											item.status == 1
												? '待总部审核'
												: item.status == 2
												? '待填写物流信息'
												: item.status == 3
												? '待总部签收'
												: item.status == 4
												? '处理中'
												: item.status === 5
												? '待总部寄出'
												: item.status == 6
												? '待网点签收'
												: item.status == 7
												? '网点已签收'
												: item.status == 8
												? '已取消'
												: ''
										}}
									</span>
								</view>
								<view class="module">{{ item.repairModule }}</view>
								<view class="repair_GoodsListVos_box">
									<view class="repair_GoodsListVos_item">
										<view class="item_left" style="color: #999">
											<uni-icons type="list" color="#999"></uni-icons>
											序列号
										</view>
										<view class="item_right" style="color: #999">
											<uni-icons type="calendar" color="#999"></uni-icons>
											商品规格
										</view>
									</view>
									<view
										class="repair_GoodsListVos_item"
										v-for="(o, i) in item.repairGoodsListVos"
										:key="i"
									>
										<view class="item_left">
											{{ o.snCode || '-' }}
										</view>
										<view class="item_right">
											{{ o.goodsSpecName || '-' }}
										</view>
									</view>
								</view>
								<!-- <view class="tip_title">
									<view class="tip">
										报单时间：<span class="tip_color">{{item.createTime}}</span>
									</view>
									<view class="tip">
										{{item.status==4?'完成时间':item.status==5?'取消时间':'更新时间'}}：<span
											class="tip_color">{{item.updateTime}}</span>
									</view>
								</view> -->
							</p>
							<view class="list-item_footer">
								<image
									class="list-item_footer_img"
									src="/static/network/order/time.svg"
									mode="aspectFit"
								></image>
								<view class="list-item_footer_time">
									报单时间：{{ item.createTime }}
								</view>
							</view>
						</view>
						<view class="no_some" v-if="title">
							{{ title }}
						</view>
					</view>
					<view
						v-if="PageTotalData[current2].dataList.length == 0"
						class="void_goods"
					>
						<image class="img" src="/static/goods.svg" mode="aspectFit"></image>
						<view class="tip">暂无工单</view>
						<p class="tip" v-if="phone2">暂无相关工单</p>
					</view>
				</view>
			</scroll-view>
		</view>

		<TimePopup
			ref="timeRef"
			@set-popup="showpopup = $event"
			:showpopup="showpopup"
			:workId="workId"
		></TimePopup>
	</view>
</template>

<script lang="ts" setup>
	import {ref} from 'vue';
	import {onUnload, onLoad} from '@dcloudio/uni-app';
	import return_fac_api from '@/util/network_api/return_fac_api.js';
	import TimePopup from '@/components/u-network/TimePopup.vue';
	import store from '@/store_network/index.js';
	const keyword = ref(''); //搜索关键词
	const current = ref(0); //当前页面
	const current2 = ref(0); //当前页面
	const timeRef = ref();
	const showpopup = ref(false);
	const items = [
		{title: '全部工单', value: 0},
		{title: '待确认', value: 3},
		{title: '处理中', value: 4},
		{title: '完成/取消', value: 7},
	];
	const OrderStatusQty = ref(null); //状态数量
	const PageTotalData = ref([
		{page: 1, total: 0, dataList: []},
		{page: 1, total: 0, dataList: []},
		{page: 1, total: 0, dataList: []},
		{page: 1, total: 0, dataList: []},
	]);
	// 0==工单列表 1==待客服确认 2==服务中 3==完成取消 4==待评价
	const onClickItem = (index: number, index2) => {
		current.value = index;
		current2.value = index2;
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 0,
		});
		getOrderList();
	};
	onUnload(() => {
		uni.hideLoading();
	});
	const title = ref('没有更多工单');
	const codePhone = ref(''); //验证码
	const phone = ref(''); //手机号
	const phone2 = ref(''); //手机号
	const selectHelpOtherCreateOrder = ref(false);
	const popupCancel = ref();
	const workId = ref(''); //工单Id
	const isCode = ref(false);
	// 获取工单状态数量
	const getOrderStatusNum = async () => {
		await return_fac_api.getUserRepairListSatusCount().then((res) => {
			if (res.code == 20000) {
				OrderStatusQty.value = res.data.row;
			}
		});
	};
	// 设置对应的工单状态数量 超过99+
	const setOrderNum = (index: any) => {
		switch (index) {
			case 0:
				var num: any = 0;
				if (OrderStatusQty.value.totalCount > 100) {
					num = '99+';
				} else {
					num = OrderStatusQty.value.totalCount;
				}
				return num;

			case 3:
				var num: any = 0;
				if (OrderStatusQty.value.waitSure > 100) {
					num = '99+';
				} else {
					num = OrderStatusQty.value.waitSure;
				}
				return num;
			case 4:
				var num: any = 0;
				if (OrderStatusQty.value.handlerCount > 100) {
					num = '99+';
				} else {
					num = OrderStatusQty.value.handlerCount;
				}
				return num;
			case 7:
				var num: any = 0;
				if (OrderStatusQty.value.finishCount > 100) {
					num = '99+';
				} else {
					num = OrderStatusQty.value.finishCount;
				}
				return num;
		}
	};

	// 下拉刷新触发
	const triggered = ref(false);
	const refresherpulling = (e) => {
		if (e.detail.deltaY < 0) return; // 防止上滑页面也触发下拉
		triggered.value = true;
	};
	const onRefresh = async () => {
		selectHelpOtherCreateOrder.value = false;
		keyword.value = '';
		phone.value = '';
		codePhone.value = '';
		phone2.value = '';
		isCode.value = false;
		current.value = 0;
		current2.value = 0;
		PageTotalData.value = [];
		for (let i = 0; i < 4; i++) {
			PageTotalData.value.push({page: 1, total: 0, dataList: []});
		}
		await Promise.all([getOrderStatusNum(), getOrderList()]).then(() => {
			uni.stopPullDownRefresh();
		});
		setTimeout(() => {
			triggered.value = false;
		}, 500);
	};
	// 滚到底部触发
	const scrolltolower = () => {
		if (
			PageTotalData.value[current2.value].total >
			PageTotalData.value[current2.value].dataList.length
		) {
			PageTotalData.value[current2.value].page =
				PageTotalData.value[current2.value].page + 1;
			title.value = '';
			uni.showLoading({
				title: '加载中...',
				mask: true,
			});
			Promise.all([getOrderStatusNum(), getOrderList()]).then(() => {
				uni.hideLoading();
			});
		} else {
			title.value = '没有更多工单';
		}
	};
	// 复制
	const copy = (val: any) => {
		uni.setClipboardData({
			data: val,
			success: function () {
				//调用方法成功
				uni.showToast({
					title: '已复制',
					icon: 'none',
				});
			},
		});
	};
	// 搜索
	const searchValue = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true,
		});
		current.value = 0;
		current2.value = 0;
		PageTotalData.value = [];
		for (let i = 0; i < 4; i++) {
			PageTotalData.value.push({page: 1, total: 0, dataList: []});
		}
		Promise.all([getOrderStatusNum(), getOrderList()]).then(() => {
			uni.hideLoading();
		});
	};

	// 详情页
	const goPage = (id) => {
		uni.navigateTo({
			url: '/pages_network/return_factory/factory_detail?id=' + id,
		});
	};

	// 打开取消弹窗
	const openCancel = (type, id) => {
		workId.value = id;
		if (type == 'cancel') {
			popupCancel.value.open();
		}
		if (type == 'time') {
			showpopup.value = true;
		}
	};
	// 获取工单列表
	const getOrderList = async () => {
		let data = {
			keyword: keyword.value.trim(), //工单单号/防伪码/商品名称/型号，支持模糊查询
			limit: 50, //	每页显示数量
			page: PageTotalData.value[current2.value].page,
			status: current.value === 0 ? '' : current.value, //	工单状态：1-待服务确认，2-服务中，3-完成/取消，4-待评价
		};
		await return_fac_api.getUserRepairList(data).then((res) => {
			if (res.code == 20000) {
				if (PageTotalData.value[current2.value].page > 1) {
					PageTotalData.value[current2.value].dataList = [
						...PageTotalData.value[current2.value].dataList,
						...res.data.rows,
					];
				} else {
					PageTotalData.value[current2.value].dataList = res.data.rows;
				}
				PageTotalData.value[current2.value].total = res.data.total;
				PageTotalData.value[current2.value].dataList = PageTotalData.value[
					current2.value
				].dataList.map((o) => {
					o.lookShow = false;
					return o;
				});
			}
		});
	};
	
	defineExpose({
		getList: () => {
			uni.showLoading({
				title: '加载中...',
				mask: true,
			});
			Promise.all([getOrderStatusNum(), getOrderList()]).then(() => {
				uni.hideLoading();
			});
		}
	})
</script>

<style lang="scss" scoped>
	.popup_way {
		height: 60vh;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #fff;
		padding-bottom: 100rpx;

		::-webkit-scrollbar {
			width: 0;
			height: 0;
			background-color: transparent;
			display: none;
		}

		.text_top_popup {
			text-align: center;
			font-size: 32rpx;
			padding: 20rpx 0;
			font-weight: bold;

			.icon {
				position: absolute;
				right: 40rpx;
				font-weight: 500;
			}
		}

		.height_list {
			height: 100%;
			overflow: auto;

			::-webkit-scrollbar {
				width: 0;
				height: 0;
				background-color: transparent;
				display: none;
			}

			.way_name {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
			}
		}
	}

	.content_box {
		background-color: #f6f7fa;
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
		display: flex;
		flex-direction: column;
		overflow: hidden;

		.top_header {
			background-color: #fff;
			width: 100%;
			// padding: 24rpx 0;

			.scan_btn {
				margin-right: 10rpx;
				position: fixed;
				right: 20rpx;
				bottom: 20vh;
				background-color: #007cff;
				border-radius: 50%;
				padding: 20rpx;
				z-index: 6;
			}

			.search_card {
				display: inline-flex;
				align-items: center;
				width: calc(100% - 48rpx);
				margin: 0 24rpx 24rpx;

				.left_s {
					flex: 1;
					display: inline-flex;
					align-items: center;
					background-color: #f3f3f3;
					border-radius: 40rpx;

					:deep(.uni-easyinput__content) {
						height: 70rpx;
						line-height: 70rpx;
						border-radius: 40rpx 0 0 40rpx;
						background-color: #fff !important;
					}

					.search_btn {
						padding: 0 20rpx;
						height: 69rpx;
						background-color: #007cff;
						border-radius: 0 40rpx 40rpx 0;
						display: flex;
						justify-content: center;
						align-items: center;
						color: #fff;
						font-size: 28rpx;
					}
				}

				.phone_btn {
					font-size: 24rpx;
					color: #007cff;
					margin-left: 10rpx;
				}
			}

			.search_phone {
				font-size: 28rpx;
				margin: 0rpx 24rpx 10rpx;
				background-color: #f3f3f3;
				display: inline-block;
				padding: 12rpx;
				border-radius: 40rpx;

				.tel {
					color: #007cff;
					margin-right: 12rpx;
				}
			}

			:deep(.segmented-control) {
				border-bottom: 1rpx solid #eee;
				box-sizing: border-box;
				display: flex;
				justify-content: space-around;
				padding: 22rpx 20rpx 0;
				background-color: #fff;

				.segmented-control__text {
					font-size: 28rpx !important;
					font-weight: bold;
					border-bottom: 4rpx solid transparent;
					padding: 10rpx;
					position: relative;
					z-index: 5;

					.text {
						background-color: #fff;
						position: relative;
						z-index: 1;
						border-radius: 10rpx;
						padding: 0 4rpx;
					}

					.red_num {
						border: 4rpx solid #ff5733;
						border-radius: 12rpx;
						min-width: 30rpx;
						height: 30rpx;
						line-height: 30rpx;
						position: absolute;
						top: -17rpx;
						right: -10rpx;
						text-align: center;
						color: #ff5733;
						font-size: 20rpx;
						transform: scale(0.9);
						z-index: -1;
						padding: 0 2rpx;
					}
				}

				.segmented-control__item--text {
					border-bottom: 4rpx solid #007cff;
					padding: 10rpx;
					color: #007cff;
					transition: all 0.5s;
				}
			}
		}

		.main_bg {
			display: flex;
			flex-direction: column;
			height: 100%;
			flex: 1;
		}

		.scroll_y {
			height: 100%;

			::-webkit-scrollbar {
				width: 0;
				height: 0;
				background-color: transparent;
				display: none;
			}
		}

		.content-list {
			// background-color: #f6f7fa;
			padding-bottom: 360rpx;
			box-sizing: border-box;

			.content-list_item {
				display: flex;
				flex-direction: column;
				row-gap: 24rpx;
			}

			.no_some {
				font-size: 24rpx;
				color: #ccc;
				text-align: center;
				width: 100%;
			}

			.c_item {
				background-color: #fff;
				margin: 0 24rpx;
				padding: 24rpx;
				border-radius: 4rpx;
				// position: relative;

				.sty_title {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.status {
						font-size: 22rpx;
						background-color: #f2f9ff;
						color: #007cff;
						padding: 8rpx 12rpx;
						border-radius: 4rpx;
					}

					.status_123 {
						color: #e6a23c;
						background-color: #fdf6ec;
					}

					.status_4 {
						color: #00c0c0;
						background-color: #f2ffff;
					}

					.status_6 {
						color: #909399;
						background-color: #f4f4f5;
					}

					.workNo {
						font-size: 28rpx;
						font-weight: bold;
						// padding: 20rpx 0 24rpx;
						display: flex;
						align-items: center;

						.btn_c {
							height: 30rpx;
							line-height: 30rpx;
							width: 24rpx;
							margin-left: 24rpx;
						}
					}
				}

				.module {
					font-size: 24rpx;
					// color: #666;
					font-weight: bold;
					// margin-left: 8rpx;
					padding: 12rpx 0 16rpx;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					word-wrap: break-word;
					word-break: break-all;
				}

				.repair_GoodsListVos_box {
					display: flex;
					flex-direction: column;

					.repair_GoodsListVos_item {
						border-top: 1px solid #eee;
						display: flex;
						align-items: center;
						column-gap: 24rpx;
						font-size: 24rpx;
						padding: 12rpx 0;
						flex: 1;
						color: #000;

						.item_left {
							flex: 1;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}

						.item_right {
							flex: 1;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}
					}
				}

				.tip_title {
					font-size: 24rpx;
					color: #777;

					.tip {
						padding-bottom: 6rpx;

						.tip_color {
							color: #000;
						}
					}
				}

				.look_some {
					text-align: center;
				}

				.btn_change {
					text-align: right;

					uni-button:after {
						height: 196%;
					}

					.btn_green {
						background-color: #fff;
						padding: 0 12rpx;
						color: #00c0c0;
						font-size: 24rpx;
						margin-left: 24rpx;
					}

					.btn_green-hover {
						background-color: #00c0c02b;
					}

					.btn_again {
						background-color: #fff;
						padding: 0 12rpx;
						color: #666;
						font-size: 24rpx;
						margin-left: 24rpx;
					}

					.btn_again-hover {
						background-color: #6666662b;
					}

					.btn_title {
						background-color: #fff;
						padding: 0 12rpx;
						color: #007cff;
						font-size: 24rpx;
						margin-left: 24rpx;
					}

					.btn_title-hover {
						background-color: #007cff2b;
					}
				}

				.list-item_footer {
					display: flex;
					align-items: center;
					color: rgba(85, 85, 85, 1);
					padding: 8rpx;
					margin-top: 12rpx;
					border-radius: 4rpx;
					background-color: rgba(246, 247, 248, 1);
					font-size: 24rpx;

					&_img {
						width: 30rpx;
						height: 30rpx;
					}

					&_time {
						margin: 0 8rpx;
					}
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 400rpx;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}
		}

		.add_main {
			background-color: #fff;
			border-radius: 10rpx;
			width: 80vw;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding: 24rpx;
			position: relative;

			.phone_tip {
				font-size: 24rpx;
				color: rgba(255, 0, 0, 1);
				padding-top: 12rpx;
			}

			.close_this {
				position: absolute;
				right: 24rpx;
				top: 24rpx;
			}

			.code_re {
				position: relative;
				width: 100%;

				.code_text {
					height: 30rpx;
					line-height: 30rpx;
					font-size: 28rpx;
					color: #007cff;
					position: absolute;
					top: 55%;
					right: 30rpx;
					z-index: 2;
				}

				.code_text_ {
					height: 30rpx;
					font-size: 28rpx;
					line-height: 30rpx;
					color: #777;
					position: absolute;
					top: 55%;
					right: 30rpx;
				}
			}

			.onlogout {
				font-size: 28rpx;
				margin: 30rpx 20rpx 0;
				height: 80rpx;
				border-radius: 50rpx;
				line-height: 80rpx;
				width: 100%;
				color: #fff;
				background-color: #007cff;
			}

			:deep(.is-input-border) {
				border-radius: 4rpx !important;
				background-color: rgba(243, 244, 248, 1) !important;
				padding: 10rpx;
				margin-top: 40rpx;
				border: none;
				height: 80rpx !important;
				line-height: 80rpx;
				font-size: 28rpx;
			}
		}
	}
</style>
